﻿using AutoMapper;
using B.S.BasicData.Api.Write.Application.Command.Approval;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.Approval
{
    public class DelApprovalHandler : IRequestHandler<DelApprovalCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<ApprovalModel> approvalRepo;
        private readonly IMapper mapper;

        public DelApprovalHandler(IBaseRepository<ApprovalModel> approvalRepo,IMapper mapper)
        {
            this.approvalRepo = approvalRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelApprovalCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var approval = approvalRepo.GetAll().Where(x => x.Id == request.Id && x.IsDelete==false).FirstOrDefault();
            approval = mapper.Map(request, approval);
            approval.IsDelete = true;
            var i = approvalRepo.Update(approval);
            if (i>0)
            {
                res.Msg = "删除成功";
                res.Code = ApiEnum.Success;
                res.Data = i;
            }
            else
            {
                res.Msg = "删除失败";
                res.Code = ApiEnum.Error;
                res.Data = i;
            }
            return Task.FromResult(res);
        }
    }
}
